﻿using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace Sidvall.Web.Security
{
	[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
	public sealed class ResourceActionAuthorizeAttribute : AuthorizeAttribute
	{
		#region Public Members

		#region Action

		private string _Action;
		public string Action
		{
			get
			{
				return _Action;
			}
			private set
			{
				_Action = value;
			}
		}

		#endregion
		#region Resources

		private IEnumerable<string> _Resources;
		public IEnumerable<string> Resources
		{
			get
			{
				return _Resources;
			}
			private set
			{
				_Resources = value;
			}
		}

		#endregion

		#region IsAuthorized

		protected override bool IsAuthorized(HttpActionContext actionContext)
		{
			if (actionContext == null)
				throw new System.ArgumentNullException("actionContext");
			return Sidvall.SystemContext.Current.AuthorizationManager.IsAuthorized(actionContext.ControllerContext.RequestContext.Principal, _Resources, _Action);
		}

		#endregion

		#endregion
		#region Constructors

		public ResourceActionAuthorizeAttribute()
		{
		}
		public ResourceActionAuthorizeAttribute(string action, params string[] resources)
		{
			this.Action = action;
			this.Resources = resources;
		}

		#endregion
	}
}
